Imports System.Text.RegularExpressions
Imports System.Threading
Imports MIMS.WRS.Entities

Public Class DateTimePicker
    Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents reqvDTPicker As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents ltrJS As System.Web.UI.WebControls.Literal
    Protected WithEvents hidSelectedValue As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents txtDTPicker As System.Web.UI.HtmlControls.HtmlInputText
    Protected WithEvents btnDTPicker As System.Web.UI.HtmlControls.HtmlImage

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private m_showsTime As Boolean = False
    Private m_dateTimeFormat As String = Config.DateFormat

#Region "support methods"

    Public ReadOnly Property DateInputClientID() As String
        Get
            Return Me.txtDTPicker.ClientID
        End Get
    End Property

    Private Function ConvertDotNetDateTimeFormatToJSCalendarDateTimeFormat(ByVal dateTimeFormat As String) As String
        dateTimeFormat = Regex.Replace(dateTimeFormat, _
                "dddd|ddd|dd|d|MMMM|MMM|MM|M|yyyy|yy|y|HH|H|hh|h|mm|m|ss|s|tt|t", _
                AddressOf ReplaceFormat)
        Return dateTimeFormat
    End Function

    Private Function ReplaceFormat(ByVal m As Match) As String
        Select Case m.Value
            Case "dddd"
                Return "%A"
            Case "ddd"
                Return "%a"
            Case "dd"
                Return "%d"
            Case "d"
                Return "%e"
            Case "MMMM"
                Return "%B"
            Case "MMM"
                Return "%b"
            Case "M", "MM"
                Return "%m"
            Case "yyyy"
                Return "%Y"
            Case "yy", "y"
                Return "%y"
            Case "HH"
                Return "%H"
            Case "H"
                Return "%k"
            Case "hh"
                Return "%I"
            Case "h"
                Return "%l"
            Case "mm", "m"
                Return "%M"
            Case "ss", "s"
                Return "%S"
            Case "tt", "t"
                Return "%p"
            Case Else
                Return ""
        End Select
    End Function
		
    Private Sub RenderSetupScript()
        Dim setupParameters As String = ""

        setupParameters += "inputField : """ + hidSelectedValue.ClientID + """"
        setupParameters += ", ifFormat : ""%Y-%m-%d %H:%M"""
        setupParameters += ", displayArea : """ + txtDTPicker.ClientID + """"
        setupParameters += ", daFormat : """ + ConvertDotNetDateTimeFormatToJSCalendarDateTimeFormat(DateTimeFormat) + """"
        setupParameters += ", button : """ + btnDTPicker.ClientID + """"
        setupParameters += ", showsTime : " + Me.showsTime.ToString().ToLower()

        Dim setupScript As String = _
          "<script type=""text/javascript"">" + _
          "    Calendar.setup({" + setupParameters + "});" + _
          "</script>"

        Me.ltrJS.Text = setupScript
    End Sub
#End Region

    Public Property RequiredText() As String
        Get
            Return reqvDTPicker.Text
        End Get
        Set(ByVal Value As String)
            reqvDTPicker.Text = Value
        End Set
    End Property

    Public Property RequiredErrorMessage() As String
        Get
            Return reqvDTPicker.ErrorMessage
        End Get
        Set(ByVal Value As String)
            reqvDTPicker.ErrorMessage = Value
        End Set
    End Property

    Public Property EnableRequiredValidator() As Boolean
        Get
            Return reqvDTPicker.Visible
        End Get
        Set(ByVal Value As Boolean)
            reqvDTPicker.Visible = Value
            reqvDTPicker.Enabled = Value
        End Set
    End Property

    Public Property ShowsTime() As Boolean
        Get
            Return m_showsTime
        End Get
        Set(ByVal Value As Boolean)
            m_showsTime = Value
        End Set
    End Property

    Public Property DateTimeFormat() As String
        Get
            If (m_dateTimeFormat = Nothing) Then
                Return Thread.CurrentThread.CurrentUICulture.DateTimeFormat.ShortDatePattern
            Else
                Return m_dateTimeFormat
            End If
        End Get
        Set(ByVal Value As String)
            m_dateTimeFormat = Value
        End Set
    End Property

    Public Property SelectedValue() As DateTime
        Get
            Dim _selectedValue As DateTime = Nulls.NullDate
            If (txtDTPicker.Value <> "") Then
                Try
                    _selectedValue = DateTime.Parse(txtDTPicker.Value, Thread.CurrentThread.CurrentUICulture.DateTimeFormat)
                Catch ex As Exception

                End Try
            End If

            Return _selectedValue
        End Get
        Set(ByVal Value As DateTime)
            If (Value.Equals(Nulls.NullDate)) Then
                txtDTPicker.Value = ""
                hidSelectedValue.Value = ""
            Else
                txtDTPicker.Value = Value.ToString(DateTimeFormat)
                hidSelectedValue.Value = Value.ToString("yyyy-MM-dd HH:mm")
            End If
        End Set
    End Property

    Public ReadOnly Property HiddenClientID() As String
        Get
            Return hidSelectedValue.ClientID
        End Get
    End Property

    Public ReadOnly Property InputClientID() As String
        Get
            Return txtDTPicker.ClientID
        End Get
    End Property

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RenderSetupScript()
    End Sub

End Class
